package com.dz.service.impl;

import com.dz.constant.MessageConstant;
import com.dz.entity.AddressBook;
import com.dz.mapper.AddressMapper;
import com.dz.service.UserAddressService;
import io.jsonwebtoken.lang.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 鄧浩奇
 * @classname UserAddressServiceImpl
 * @description
 * @since 2024/4/4 17:12
 **/

@Service
public class UserAddressServiceImpl implements UserAddressService {

    private final AddressMapper addressMapper;

    @Autowired
    public UserAddressServiceImpl(AddressMapper addressMapper) {
        this.addressMapper = addressMapper;
    }

    /*
    * 新增地址
    * */
    @Override
    public Integer addAddress(AddressBook addressBook) {
        Assert.hasText(addressBook.getConsignee(), MessageConstant.PARAM_ERROR);
        Assert.hasText(addressBook.getPhone(), MessageConstant.PARAM_ERROR);
        Assert.hasText(addressBook.getCityName(), MessageConstant.PARAM_ERROR);
        Assert.hasText(addressBook.getDistrictName(), MessageConstant.PARAM_ERROR);
        Assert.hasText(addressBook.getProvinceName(), MessageConstant.PARAM_ERROR);
        Assert.hasText(addressBook.getDetail(), MessageConstant.PARAM_ERROR);

        return addressMapper.insertAddress(addressBook);

    }

    /*
    * 查询当前登陆的用户的所有地址
    * */
    @Override
    public List<AddressBook> getAddressesByUserId(Long userID) {
        return addressMapper.selectAddressesByUserID(userID);
    }

    /*
    * 设置默认地址
    * */
    @Override
    public Integer setDefaultByAddressId(Long userId,Integer addressID) {
        return addressMapper.updateDefaultByAddressId(userId,addressID);
    }

    /*
    * 获取默认地址
    * */
    @Override
    public AddressBook getDefaultByUserId(Long userID) {
        return addressMapper.selectDefaultByUserId(userID);
    }

    /*
    * 根据id查询地址信息
    * */
    @Override
    public AddressBook getAddressById(Long id) {
        return addressMapper.selectAddressById(id);
    }

    /*
    * 根据id修改地址
    * */
    @Override
    public Integer updateById(AddressBook addressBook) {
        return addressMapper.updateAddressById(addressBook);
    }

    @Override
    public Integer deleteById(Long id) {
        return addressMapper.deleteById(id);
    }
}
